package com.hit.aircraftwar.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SwitchCompat;

import com.hit.aircraftwar.R;
import com.hit.aircraftwar.setter.ParameterSet;

public class SettingActivity extends AppCompatActivity {

    private Button settingsReturnButton;
    private RadioGroup difficultyRadioGroup;
    private SwitchCompat soundSwitch;
    private SwitchCompat bgmSwitch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        //返回按钮
        settingsReturnButton = findViewById(R.id.settingsReturnButton);
        settingsReturnButton.setOnClickListener(view -> {
            Intent intent = new Intent(SettingActivity.this, MainActivity.class);
            startActivity(intent);
        });

        //修改难度
        difficultyRadioGroup = findViewById(R.id.difficultyRadioGroup);
        difficultyRadioGroup.setOnCheckedChangeListener(
                (radioGroup, i) -> {
                    int len = difficultyRadioGroup.getChildCount();
                    for(int j = 0; j < len; ++j) {
                        RadioButton radioButton = (RadioButton) difficultyRadioGroup.getChildAt(j);
                        if(radioButton.isChecked()){
                            ParameterSet.getInstance().setDifficulty(j);
                            View view = findViewById(R.id.settingLayout);
                            view.setBackgroundResource(ParameterSet.INSTANCE.getBackGround());
                            break;
                        }
                    }
                }
        );

        //修改声音
        bgmSwitch = findViewById(R.id.bgmSwitch);
        soundSwitch = findViewById(R.id.soundSwitch);
        bgmSwitch.setOnClickListener(view -> ParameterSet.getInstance().setBgm(bgmSwitch.isChecked()));
        soundSwitch.setOnClickListener(view -> ParameterSet.getInstance().setSound(soundSwitch.isChecked()));

        //显示当前的设置
        RadioButton radio = (RadioButton) difficultyRadioGroup.getChildAt(ParameterSet.INSTANCE.getDifficulty());
        radio.setChecked(true);
        bgmSwitch.setChecked(ParameterSet.getInstance().getBgm());
        soundSwitch.setChecked(ParameterSet.INSTANCE.getSound());
    }
}
